/**
 * PhoneReceiver.java
 * 
 * Create Version: 1.0
 * Author: lixh
 * Create Date: 2014-9-7
 * 
 * Copyright (c) 2014 CQCIS. All Right Reserved.
 */
package com.yinhai.receiver;

import java.util.List;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cmcc.groupcontacts.MainApp;
import com.yinhai.activity.R;
import com.yinhai.model.ContactsUser;
import com.yinhai.model.OnlineContactDetail;
import com.yinhai.service.ContactUserServiceImpl;

/**
 * PhoneReceiver （电话拦截）
 * 
 * @author lixh
 */
public class PhoneReceiver extends BroadcastReceiver {

	private static final String TAG = "PhoneReceiver";
	private Context context;
	private static boolean incomingFlag = false;
	private static String incoming_number = null;

	@Override
	public void onReceive(Context context, Intent intent) {
		// 如果是拨打电话
		if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
			incomingFlag = false;
			String phoneNumber = intent
					.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
			
			MainApp.instance.initWindowManager();
			View windowView = MainApp.instance.windowView;
			
			TextView textView = (TextView) windowView
					.findViewById(R.id.name);
			List<ContactsUser> list = new ContactUserServiceImpl(context)
					.findContactDispalyName(phoneNumber);
			if (list != null && list.size() > 0) {
				OnlineContactDetail detail = new ContactUserServiceImpl(
						context).findOnlinePhoneContactDetail(list.get(0)
						.getTelPhone());
				TextView company = (TextView) windowView
						.findViewById(R.id.company);
				if (detail.getCompany() != null) {
					company.setText(detail.getCompany());
				} else {
					company.setVisibility(View.GONE);
				}
				TextView duty = (TextView) windowView
						.findViewById(R.id.duty);
				if (detail.getDuty() != null) {
					duty.setText(detail.getDuty());
				} else {
					duty.setVisibility(View.GONE);
				}
				textView.setText(list.get(0).getName());
			} else {
				textView.setText("未知号码");
			}
			textView = (TextView) windowView.findViewById(R.id.phone);
			textView.setText("呼出号码：" + phoneNumber);
			
			
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Service.TELEPHONY_SERVICE);
			tm.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE);
			// tm.getSubscriberId()
			
		} else {
			// 如果是来电
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Service.TELEPHONY_SERVICE);
			// tm.getSubscriberId()
			switch (tm.getCallState()) {
			case TelephonyManager.CALL_STATE_RINGING:

				incomingFlag = true;// 标识当前是来电

				incoming_number = intent.getStringExtra("incoming_number");
				MainApp.instance.initWindowManager();
				View windowView = MainApp.instance.windowView;
				TextView textView = (TextView) windowView
						.findViewById(R.id.name);
				List<ContactsUser> list = new ContactUserServiceImpl(context)
						.findContactDispalyName(incoming_number);
				if (list != null && list.size() > 0) {
					OnlineContactDetail detail = new ContactUserServiceImpl(
							context).findOnlineContactDetail(list.get(0)
							.getUserid());
					TextView company = (TextView) windowView
							.findViewById(R.id.company);
					if (detail.getCompany() != null) {
						company.setText(detail.getCompany());
					} else {
						company.setVisibility(View.GONE);
					}
					TextView duty = (TextView) windowView
							.findViewById(R.id.duty);
					if (detail.getDuty() != null) {
						duty.setText(detail.getDuty());
					} else {
						duty.setVisibility(View.GONE);
					}
					textView.setText(list.get(0).getName());
				} else {
					textView.setText("未知来电");
				}
				textView = (TextView) windowView.findViewById(R.id.phone);
				textView.setText("来电号码：" + incoming_number);

				break;

			case TelephonyManager.CALL_STATE_OFFHOOK:

				if (incomingFlag) {


				}

				break;

			case TelephonyManager.CALL_STATE_IDLE:
				try {
					if (MainApp.instance.wm != null
							&& MainApp.instance.windowView != null) {
						MainApp.instance.wm
								.removeViewImmediate(MainApp.instance.windowView);
						MainApp.instance.wm = null;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

				break;

			}

		}
	}
	
	public class TelListener extends PhoneStateListener {  
		  
	    @Override  
	    public void onCallStateChanged(int state, String incomingNumber) {  
	        super.onCallStateChanged(state, incomingNumber);  
	  
	        switch (state) {  
	        case TelephonyManager.CALL_STATE_IDLE: // 空闲状态，即无来电也无去电  
	            //此处添加一系列功能代码 
	            break;  
	        case TelephonyManager.CALL_STATE_RINGING: // 来电响铃  
	            //此处添加一系列功能代码 
	            break;  
	        case TelephonyManager.CALL_STATE_OFFHOOK: // 摘机，即接通 
	            //此处添加一系列功能代码 
	            break;  
	        }  
	  
	    }  
	  
	}

}
